package com.abing.starry.job.cycle;

import com.abing.starry.model.entity.InterfaceInfoEntity;
import com.abing.starry.service.InterfaceInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

import static com.abing.starry.constant.RedisKeyConstant.INTERFACE_COUNT_DAY;

/**
 * @Author CaptainBing
 * @Date 2024/8/31 23:55
 * @Description
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class InterfaceDayCountJob {

    private final StringRedisTemplate stringRedisTemplate;

    private final InterfaceInfoService interfaceInfoService;

    /**
     * 每日凌晨更新接口当日调用次数
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void dayCount() {

        List<InterfaceInfoEntity> interfaceInfoEntityList = interfaceInfoService.list();
        Long deleteCount = stringRedisTemplate.opsForHash().delete(INTERFACE_COUNT_DAY, interfaceInfoEntityList
                .stream()
                .map(InterfaceInfoEntity::getId)
                .toArray());
        log.info("时间:{},接口数量:{},刷新成功数量:{}", LocalDateTime.now(), interfaceInfoEntityList.size(), deleteCount);

    }

//    @Scheduled(fixedRate = 5000)
    public void doCycleDemoJob(){
        stringRedisTemplate.opsForHash().delete(INTERFACE_COUNT_DAY, interfaceInfoService
                .list()
                .stream()
                .map(InterfaceInfoEntity::getId)
                .toArray());
        log.info("循环执行示例 每5秒执行一次");
    }

}
